package com.chen.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.dto.goodsAddParam;
import com.chen.common.dto.goodsParam;
import com.chen.common.dto.pageParam;
import com.chen.common.mbg.mapper.GoodsMapper;
import com.chen.common.mbg.pojo.Goods;
import com.chen.common.mbg.service.GoodsService;
import com.chen.common.utils.ApiResponse;
import com.chen.common.utils.BeanUtil;
import com.chen.common.vo.goodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品展示
 */
@Api(tags = "goodsController", value = "商品管理")
@RestController
@RequestMapping("goods")
public class goodsController {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsService goodsService;

    @ApiOperation("获取商品信息列表,通过商品名")
    @PostMapping("/selectOne")
    public ApiResponse selectOne(@RequestBody goodsParam po) {

//        List<goodsVo> goodsVos = BeanUtil.listA2ListB(goodsPage.getRecords(), goodsVo.class);
        //beanUtil转换成vo不成功
        List<Goods> goodsList = goodsMapper.selectList(new LambdaQueryWrapper<Goods>().like(Goods::getGName, po.getGName()));
        List<goodsVo> goodsVos = BeanUtil.listA2ListB(goodsList, goodsVo.class);
        return new ApiResponse(goodsVos);
    }
    /**
     *
     * @param po
     * 浏览器中的参数
     * {
     *     "pageNum": "1",
     *     "pageSize":"5",
     *     "gid":""
     *     "gname":"",
     *     "gcategory":"手书"
     * }
     * 分类查询
     * @return
     */
    @ApiOperation("获取商品信息列表")
    @PostMapping("/listAll")
    public ApiResponse listAll(@RequestBody goodsParam po) {

//        List<goodsVo> goodsVos = BeanUtil.listA2ListB(goodsPage.getRecords(), goodsVo.class);
        //beanUtil转换成vo不成功
        List<goodsVo> goodsVos = goodsService.listAll(po);
        return new ApiResponse(goodsVos);
    }

    @ApiOperation("添加商品")
    @PostMapping("addGood")
    public ApiResponse addGood(@RequestBody goodsAddParam po){
        Goods goods = BeanUtil.beanA2beanB(po, Goods.class);
        goodsMapper.insert(goods);
        return new ApiResponse("添加成功");
    }

    @ApiOperation("撤销商品")
    @DeleteMapping("delGood")
    public ApiResponse delGood(@RequestParam Integer id){
        goodsMapper.deleteById(id);
        return new ApiResponse("删除成功");
    }

    @ApiOperation("修改商品")
    @PutMapping("updateGood")
    public ApiResponse updateGood(@RequestBody goodsAddParam po){
        Goods goods = BeanUtil.beanA2beanB(po, Goods.class);
        goodsMapper.updateById(goods);
        return new ApiResponse("修改成功");
    }

    @GetMapping("/test")
    public String success(){
        return "success";
    }
}
